<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<title>Model Backup Demo</title>
        <style type='text/css'>
            body {font-family: verdana}
            li {border: solid 1px gray; padding: 5px; width: 250px;}
			li a {color: red; font-weight: bold;}
			p {width: 400px;}
        </style>
	</head>
	<body>
		<div id="demo-instructions">
		<h1>Model Backup Demo</h1>
			<p>This demo shows backing up and restoring model instances.
			Click the names to change birthdays.  After changing some birthdays,
			click RESTORE.  
			</p>
		</div>
<div id="demo-html">
<div id='contacts'></div>
<div id='update'></div>
<a href='javascript://' id='restore'>RESTORE</a>
</div>
<script type='text/javascript' 
        src='../../../steal/steal.js'>   
</script>
<script type='text/javascript'>
steal('jquery/model',
		'jquery/dom/fixture',
		'jquery/model/list',
		'jquery/model/backup').then(function(){
			
			$.fixture("/recipes.json", function(orig, settings, headers){
				return [[{'id': 1,'name' : 'Justin Meyer','birthday': '1982-10-20'},
						 {'id': 2,'name' : 'Brian Moschel','birthday': '1983-11-10'},
						 {'id': 3,'name' : 'Alex Gomes','birthday': '1980-2-10'}]];
			});			

			
$.Model("Contact",{
	attributes : { 
		birthday : 'date'
	},
	convert : {
		date : function(raw){
			if(typeof raw == 'string'){
				var matches = raw.match(/(\d+)-(\d+)-(\d+)/)
				return new Date( +matches[1], 
				                 (+matches[2])-1, 
				                 +matches[3] )
			}else if(raw instanceof Date){
				return raw;
			}
		}
	},
	findAll : function(params, success, error){
		return $.get("/recipes.json", {}, success, "json contact.models");
	},
	update : function(id, attrs, success, error){
		$.post("/recipes.json",{},success,'json',function(){
			return [attrs]
		})
	}
},{
	ageThisYear : function(){
		return new Date().getFullYear() - 
		      this.birthday.getFullYear()
	},
	getBirthday : function(){
		return ""+this.birthday.getFullYear()+
			"-"+(this.birthday.getMonth()+1)+
			"-"+this.birthday.getDate();
	}

});
makeAgeUpdater = function(contact){
	var updater = $("#update")
	updater.html("");
	updater.append(contact.name+"'s birthday")
	$('<input/>').val(contact.attr("birthday")).change(function(){
		contact.update({
			'birthday': this.value
		})
	}).appendTo(updater)
}
$('#contacts').delegate("li","click", function(){
	 makeAgeUpdater( $(this).closest('.contact').model() );
  });
Contact.findAll({},function(contacts){
  var contactsEl = $('#contacts');
  $.each(contacts, function(i, contact){
    
	// add the contact to the page
	var li = $('<li>')
              .model(contact)
              .html(contact.name+" "+
			  		contact.ageThisYear()+
                    " <a>Show</a>")
              .appendTo(contactsEl);
    
	// listen for changes in birthday
	contact.bind("birthday", function(){
      li.html(contact.name+" "+this.ageThisYear()+
              " <a>Show</a>");
    })
	
	// backup the contact
	contact.backup();
  })
  
  // on restore, update all contacts
  $("#restore").click(function(){
  	contacts.each(function(){
		this.restore()
	})
  })
});
})
</script>
	</body>
</html>